home *** CD-ROM | disk | FTP | other *** search
/ PC Elektro 3 / PC-Elektro-3-cd1.bin / KBan 2.0 / KBANSRC.LZH / SRC / PROG / COMMON / RCNTSTAT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-17  |  3.2 KB  |  138 lines

  1. //
  2. // template class RecentStates
  3. // Copyright (C) 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
  4. //
  5.  
  6. // This class is a container class useful for storing "undo"
  7. // and "redo" history in an application.
  8. //
  9. // Template class RecentStates requires that class T has the
  10. // following member functions.
  11. //
  12. //   T::T()
  13. //   T::T(const T&)
  14. //
  15. // Depending on the implementation of class list, the
  16. // following member functions may need to be defined but are
  17. // not used actually.
  18. //
  19. //   bool operator==(const Document& rval) const;
  20. //   bool operator!=(const Document& rval) const;
  21. //   bool operator<(const Document& rval) const;
  22. //   bool operator>(const Document& rval) const;
  23.  
  24. #ifndef _RCNTSTAT_H_
  25. #define _RCNTSTAT_H_
  26.  
  27. #include <list>
  28. #include <string>
  29.  
  30. template<class T>
  31. class StateNode {
  32.   // member variable(s)
  33.   T m_data;
  34.   std::string m_name;
  35. public:
  36.   // constructor(s)
  37.   StateNode() {
  38.   }
  39.   StateNode(const T& data, const std::string& name)
  40.     : m_data(data),
  41.       m_name(name) {
  42.   }
  43.  
  44.   // destructor
  45.   ~StateNode() {}
  46.  
  47.   // member functions
  48.   bool operator==(const StateNode& rval) const;
  49.   bool operator!=(const StateNode& rval) const;
  50.   bool operator<(const StateNode& rval) const;
  51.   bool operator>(const StateNode& rval) const;
  52.   const T& data() const { return m_data; }
  53.   const std::string& name() const { return m_name; }
  54. };
  55.  
  56. template<class T>
  57. class RecentStates {
  58.   // member variable(s)
  59.   typedef StateNode<T> Node;
  60.   typedef std::list<Node> List;
  61.   List m_states;
  62.   List::iterator m_current;
  63.   std::string null_str;
  64. public:
  65.   // constructor(s)
  66.   RecentStates() {
  67.   }
  68.   RecentStates(const T& initial_state) {
  69.     m_states.push_back(Node(initial_state, ""));
  70.     m_current = m_states.begin();
  71.   }
  72.  
  73.   // destructor
  74.   virtual ~RecentStates() {}
  75.  
  76.   // member functions
  77.   bool IsUndoable() const {
  78.     return (m_current != m_states.begin()) ? true : false;
  79.   }
  80.   bool IsRedoable() const {
  81.     List::iterator temp = m_current;
  82.     temp++;
  83.     return (temp != m_states.end()) ? true : false;
  84.   }
  85.   bool Undo() {
  86.     if(!IsUndoable()) {
  87.       return false;
  88.     }
  89.     m_current--;
  90.     return true;
  91.   }
  92.   bool Redo() {
  93.     if(!IsRedoable()) {
  94.       return false;
  95.     }
  96.     m_current++;
  97.     return true;
  98.   }
  99.   void ClearHistory() {
  100.     if(IsUndoable()) {
  101.       List::iterator temp = m_current;
  102.       m_states.erase(m_states.begin(), m_current);
  103.     }
  104.     if(IsRedoable()) {
  105.       List::iterator temp = m_current;
  106.       temp++;
  107.       m_states.erase(temp, m_states.end());
  108.     }
  109.   }
  110.   bool SetNewState(const T& new_state, const std::string& name) {
  111.     List::iterator temp = m_current;
  112.     temp++;
  113.     m_states.erase(temp, m_states.end());
  114.     m_states.push_back(Node(new_state, name));
  115.     m_current++;
  116.     return true;
  117.   }
  118.   const T& GetCurrent() const { return m_current->data(); }
  119.   const std::string& GetUndoName() const {
  120.     if(IsUndoable()) {
  121.       return m_current->name();
  122.     } else {
  123.       return null_str;
  124.     }
  125.   }
  126.   const std::string& GetRedoName() const {
  127.     if(IsRedoable()) {
  128.       List::iterator temp = m_current;
  129.       temp++;
  130.       return temp->name();
  131.     } else {
  132.       return null_str;
  133.     }
  134.   }
  135. };
  136.  
  137. #endif /* _RCNTSTAT_H_ */
  138.